﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace SilverAnts.Utilities
{
    /// <summary>
    /// 注册使用
    /// </summary>
    public class Ticket
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="path">licence文件路径</param>
        /// <returns></returns>
        public static TicketInfo License(string path)
        {
            string key = "201512255210";
            return License(path, key);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="path">licence文件路径</param>
        /// <param name="key">key值</param>
        public static TicketInfo License(string path, string key)
        {
            var ticketInfo = new TicketInfo()
            {
                Allow = false,
                MachineCode = GetMachineCode(key)
            };
            //存在 
            if (!File.Exists(path))
            {
                return ticketInfo;
            }
            try
            {
               //行数据
                var lines = new List<string>();
                using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
                {
                    while (sr.Peek() >= 0)
                    {
                        lines.Add(sr.ReadLine());
                    }
                    sr.Close();
                    sr.Dispose();
                }
                //是否是在同一个机器
                var MachineCode = Security.DESEncrypt(ticketInfo.MachineCode + key);
                if (MachineCode == lines[0])
                {
                    /*
                    ticketInfo.ProductNo = Security.DESDecrypt(lines[1], key);
                    ticketInfo.DueDate = DecryptString(lines[2], key);
                    ticketInfo.RegDate = DecryptString(lines[3], key);
                    int count = Convert.ToInt32(DecryptString(lines[4], key));
                    ticketInfo.UserCount = count == 0 ? int.MaxValue : count;
                    ticketInfo.Trial = DecryptString(lines[5], key).Contains("0") ? true : false;
                    ticketInfo.Customer = DecryptString(lines[6], key);
                    if (Convert.ToDateTime(ticketInfo.DueDate) > DateTime.Now)
                    {
                        ticketInfo.Allow = true;
                    }*/
                }
                return ticketInfo;
            }
            catch
            {
                return new TicketInfo() { Allow = false, MachineCode = "-" };
            }
        }
        /// <summary>
        /// 序列号
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetMachineCode(string key)
        {
            /*
            int code = Convert.ToInt32(key.Replace("CiCi", "")) + 19880203;
            var key = new int[] { (code % 4) + 1, (code % 3) + 4, (code % 2) + 1, (code % 1) + 3 };
            var serial = HardwareInfo.Serials;
            string M_1 = serial[0].Substring(serial[0].Length % key[0], 4);
            string M_2 = serial[1].Substring(serial[1].Length % key[1], 4);
            string M_3 = serial[2].Substring(serial[2].Length % key[2], 4);
            string M_4 = serial[3].Substring(serial[3].Length % key[3], 4);
            return M_2 + "-" + M_4 + "-" + M_1 + "-" + M_3;
             * */

            return "";
        }
    }

}
